/**
 * 财务中心 - 收支对账
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var angular = require("angular");
    require("angular-ui");
    var util = require("util");
    var ui = require("ui");
    var dialog = require("dialog");
    var datepicker = require("datepicker");
    require("angular-interceptor"); // 接管 ajax 错误处理
    require('authorize');
    require("permission");

    var app = angular.module('app', ['ngUI', 'ngInterceptor']);
    var api_prefix = G_CONFIG["api_prefix"] + "qmyx/payment-manage/"; //api前缀

    // 查询服务
    function filterService($http) {
        var service = {
            // 起始时间
            startDate: '',
            // 结束时间
            endDate: '',
            data: [],
            hasData: true,
            pageIndex: 1,
            total : 0,
            // 搜索关键字
            searchValue: "",
            fundAmount: "",
            payedAmount: "",
            balanceAmount: "",
            detail: [],
            // 查询参数
            queryParam: {},
            doQuery: function (pageIndex) {
                service.pageIndex = service.queryParam.filter.page_index = pageIndex || 1;
                service.queryParam.filter.page_size = service.pageSize;
                // this.queryParam = {
                //     filter: {
                //         page_index: this.pageIndex,
                //         page_size: this.pageSize,
                //         serial_number: this.searchValue,
                //         trade_date_from: this.startDate,
                //         trade_date_to: this.endDate
                //     }
                // };
                var url = api_prefix + 'get-order-list';
                $http.post(url, this.queryParam)
                        .success(function (data) {
                            service.data = data.data.data;
                            var json = service.data;

                            service.fundAmount = json.fundAmount;
                            service.payedAmount = json.payedAmount;
                            service.balanceAmount = json.balanceAmount;
                            service.detail = json.tradeDetail;
                            service.total = json.total;
                            
                            if( !service.detail || service.detail.length === 0){
                                $(".no-data").show();
                                return
                            }else{
                                $(".no-data").hide();
                            }
                            // if (isPagination === undefined) {
                            //     isPagination = true;
                            //     // $('#pagination').pagination(json.total, {
                            //     //     num_edge_entries: 1, //边缘页数
                            //     //     num_display_entries: 4, //主体页数
                            //     //     callback: function (page_index, jq) {
                            //     //         //page_index += 1; // 分页控件从 0 开始，后台从 1 开始
                            //     //         //if (isPagination === true && page_index !== service.pageIndex) {
                            //     //             service.pageIndex = page_index;
                            //     //             service.queryParam.filter.page_index = page_index;
                            //     //             service.doQuery(true);
                            //     //         //}
                            //     //     },
                            //     //     items_per_page: service.pageSize //每页显示1项
                            //     // });
                            // }
                        })
                        .finally(function () {
                            service.queryParam.filter = jQuery.parseJSON(service.queryParam.filter);
                        });
            },
            // 导出
            exportData: function () {
                var filters = service.queryParam.filter;
                //delete filters.page_index;
                //delete filters.page_size;
                var url = api_prefix + 'order-export-excel';
                url += '&filter=' + encodeURIComponent(filters);
                window.location.href = url;
            },
            // 通过日期查询
            queryDataByDate: function () {
                var filters = service.queryParam.filter;
                filters.trade_date_from = this.startDate;
                filters.trade_date_to = this.endDate;
                filters.serial_number = "";
                filters.page_index = 1;
                this.searchValue = "";
                this.doQuery();
            },
            // 通过关键字查询
            queryDataBySearch: function () {
                var filters = service.queryParam.filter;
                filters.serial_number = this.searchValue;
                filters.page_index = 1;
                filters.trade_date_from = "";
                filters.trade_date_to = "";
                this.startDate = "";
                this.endDate = "";
                this.doQuery();
            },
            // 普通查询
            queryData: function () {
                this.queryParam = {
                    filter: {
                        page_index: this.pageIndex,
                        page_size: this.pageSize,
                        serial_number: this.searchValue,
                        trade_date_from: this.startDate,
                        trade_date_to: this.endDate
                    }
                };
                this.doQuery();
            }
        }
        return service;
    }
    app.factory('filters', ['$http', filterService]);

    function queryService($scope, filters) {
        $scope.data = filters;
        setTimeout(function() {
            filters.queryData();
        }, 0);
        $scope.queryDataByDate = function () {
            filters.queryDataByDate();
        }
        $scope.queryDataBySearch = function () {
            filters.queryDataBySearch();
        }
        $scope.exportData = function () {
            filters.exportData();
        }
    };
    app.controller('queryService', ['$scope', 'filters', queryService])

    angular.bootstrap(document, ['app']);
});